package com.core.family.admin.service;


import com.core.common.gen.IBaseService;
import com.core.family.admin.domain.FileLogic;
import com.core.family.admin.domain.FileStorage;
import lombok.SneakyThrows;
import org.springframework.util.DigestUtils;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Optional;

/**
 * @author hl
 * @version 1.0.0
 * @ClassName IStorageDbService.java
 * @Description 类描述  文件存储数据库记录
 * @createTime 2023年05月05日 10:56:00
 */
public interface FileStorageService extends IBaseService<FileStorage> {


    /***
     * 新增文件
     * @param inputStream   文件输入流
     * @param suffixName       后缀名称
     * @return
     */
    default FileStorage createWithcheck(InputStream inputStream, String suffixName) throws IOException {
        return createWithcheck(inputStream, suffixName, inputStream.available());
    }

    FileStorage createWithcheck(InputStream inputStream, String suffixName, long contentLength) throws IOException;


    /***
     *
     * @param inputStream
     * @return
     * @throws IOException
     */
    default FileStorage getIfAbsent(InputStream inputStream) throws IOException {
        String md5 = DigestUtils.md5DigestAsHex(inputStream);
        return Optional.ofNullable(getByMd5(md5)).orElse(null);
    }


    boolean removeBy(String md5, String id);


    FileStorage getByMd5(String id);


}
